<template>
  <div class="employees-detail-container">
    <div class="app-container">
      <el-card>
        <a href="#" @click="$router.back()">返回</a>
        <!-- 放置表单 -->
        <el-form
          ref="userForm"
          label-width="120px"
          style="margin-left: 120px; margin-top: 30px"
          :model="userInfo"
          :rules="rules"
        >
          <el-form-item label="姓名:" prop="username">
            <el-input v-model="userInfo.username" style="width: 300px" />
          </el-form-item>
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" style="width: 300px" disabled />
          </el-form-item>
          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
            />
          </el-form-item>
          <el-form-item label="员工头像">
            <ImageUpload ref="staffPohotForm" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="Updatafn">更新</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { UpdataADDUserInfoAPI } from '@/api/employuees'
import { getUserInfoAPI } from '@/api/user'
export default {
  name: 'EmployeesDetail',
  data() {
    return {
      userInfo: {
        staffPhoto: '',
        username: '',
        mobile: '',
        timeOfEntry: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不可为空', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不可为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    staffForm() {
      return this.$refs.staffPohotForm.fileList.every(item => item.status === 'success')
    }
  },
  created() {
    this.getPeopleInfo()
  },
  methods: {
    async getPeopleInfo() {
      const { data } = await getUserInfoAPI(this.$route.params.id)
      console.log(data)
      this.userInfo = data
      this.$refs.staffPohotForm.fileList = [{ url: data.staffPhoto }]
    },
    async Updatafn() {
      const resetStaff = this.$refs.staffPohotForm
      const url = resetStaff.fileList[0]?.url
      if (!url) {
        this.$message.warning('图片不能为空')
        return
      }
      if (!this.staffForm) {
        this.$message.warning('图片正在上传，请稍后继续提交')
        return
      }
      this.userInfo.staffPhoto = url
      const res = await UpdataADDUserInfoAPI(this.userInfo)
      console.log(res)
      this.$message.success('更新成功')
    }
  }
}
</script>

<style scoped>

</style>
